/*
 * 文件名称: ClientUtils.java Copyright 2011-2015 Ximalaya All right reserved.
 */
package top.zxybird.common.util;

import top.zxybird.common.constants.ClientType;
import top.zxybird.common.constants.DeviceType;
import top.zxybird.common.model.ClientInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * @author cossea
 */
public final class ClientUtils {

  public static ClientInfo assembleClientInfo(HttpServletRequest request) {
    ClientInfo clientInfo = new ClientInfo();
    clientInfo.setChannel(RequestUtils.getCurrentChannelId(request));
    clientInfo.setDevice(DeviceType.getTypeByNameWithNull(RequestUtils.getCurrentDevice(request)));
    ClientType clientType = getClientTypeByDeviceType(DeviceType.getTypeByNameWithNull(RequestUtils
        .getCurrentDevice(request)));
    clientInfo.setClientType(clientType);
    clientInfo.setDeviceId(RequestUtils.getCurrentDeviceId(request));
    clientInfo.setIp(RequestUtils.getClientIp(request));
    clientInfo.setLatitude(RequestUtils.getCurrentPostionx(request));
    clientInfo.setLongitude(RequestUtils.getCurrentPostiony(request));
    clientInfo.setMac(RequestUtils.getCurrentMac(request));
    clientInfo.setUserAgent(RequestUtils.getUserAgent(request));
    clientInfo.setVersion(RequestUtils.getCurrentVersion(request));
    clientInfo.setBundleId(RequestUtils.getCurrentBundleId(request));
    return clientInfo;
  }

  public static ClientType getClientTypeByDeviceType(DeviceType deviceType) {
    if (null == deviceType) {
      return ClientType.mobile;
    }
    if (deviceType == DeviceType.ipad || deviceType == DeviceType.iphone) {
      return ClientType.mobile_ios;
    } else if (deviceType == DeviceType.android || deviceType == DeviceType.chezai_android) {
      return ClientType.mobile_android;
    } else if (deviceType == DeviceType.wp) {
      return ClientType.mobile_wp;
    } else {
      return ClientType.mobile;
    }
  }
}
